; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -passes=loop-idiom -S %s | FileCheck %s

target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx11.0.0"

; Make sure the number of bytes is computed correctly in the presence of zero
; extensions and preserved add flags.
define void @test(ptr %ptr) {
; CHECK-LABEL: @test(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    br label [[FOR_BODY_PREHEADER:%.*]]
; CHECK:       dead:
; CHECK-NEXT:    br label [[FOR_BODY_PREHEADER]]
; CHECK:       for.body.preheader:
; CHECK-NEXT:    [[LIM_0:%.*]] = phi i32 [ 65, [[ENTRY:%.*]] ], [ 1, [[DEAD:%.*]] ]
; CHECK-NEXT:    [[UGLYGEP:%.*]] = getelementptr i8, ptr [[PTR:%.*]], i64 8
; CHECK-NEXT:    [[UMAX:%.*]] = call i32 @llvm.umax.i32(i32 [[LIM_0]], i32 2)
; CHECK-NEXT:    [[TMP0:%.*]] = add nsw i32 [[UMAX]], -1
; CHECK-NEXT:    [[TMP1:%.*]] = zext i32 [[TMP0]] to i64
; CHECK-NEXT:    [[TMP2:%.*]] = shl nuw nsw i64 [[TMP1]], 3
; CHECK-NEXT:    call void @llvm.memset.p0.i64(ptr align 8 [[UGLYGEP]], i8 0, i64 [[TMP2]], i1 false)
; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
; CHECK:       for.body:
; CHECK-NEXT:    [[IV:%.*]] = phi i32 [ [[IV_NEXT:%.*]], [[FOR_BODY]] ], [ 1, [[FOR_BODY_PREHEADER]] ]
; CHECK-NEXT:    [[IV_EXT:%.*]] = zext i32 [[IV]] to i64
; CHECK-NEXT:    [[GEP:%.*]] = getelementptr inbounds i64, ptr [[PTR]], i64 [[IV_EXT]]
; CHECK-NEXT:    [[IV_NEXT]] = add nuw nsw i32 [[IV]], 1
; CHECK-NEXT:    [[CMP64:%.*]] = icmp ult i32 [[IV_NEXT]], [[LIM_0]]
; CHECK-NEXT:    br i1 [[CMP64]], label [[FOR_BODY]], label [[EXIT:%.*]]
; CHECK:       exit:
; CHECK-NEXT:    ret void
;
entry:
  br label %for.body.preheader

dead:
  br label %for.body.preheader

for.body.preheader:
  %lim.0 = phi i32 [ 65, %entry ], [ 1, %dead ]
  br label %for.body

for.body:
  %iv = phi i32 [ %iv.next, %for.body ], [ 1, %for.body.preheader ]
  %iv.ext = zext i32 %iv to i64
  %gep = getelementptr inbounds i64, ptr %ptr, i64 %iv.ext
  store i64 0, ptr %gep, align 8
  %iv.next = add nuw nsw i32 %iv, 1
  %cmp64 = icmp ult i32 %iv.next, %lim.0
  br i1 %cmp64, label %for.body, label %exit

exit:
  ret void
}
